package cc.mxtt.mvc.controller;

import java.util.HashMap;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.aop.Invocation;
import com.jfinal.kit.JsonKit;

import cc.mxtt.common.core.BaseController;
import cc.mxtt.common.core.GlobalKey;
import cc.mxtt.mvc.interceptor.DeptLogInterceptor;
import cc.mxtt.mvc.model.Dept;
import cc.mxtt.mvc.model.Role;
import cc.mxtt.mvc.model.RoleMenu;
import cc.mxtt.mvc.model.RolePower;
import cc.mxtt.mvc.model.User;
import cc.mxtt.mvc.model.UserRole;
import cc.mxtt.mvc.service.DeptService;
import cc.mxtt.mvc.service.impl.DeptServiceImpl;

public class DeptController extends BaseController{
	DeptService deptService = new DeptServiceImpl();
	
	public void index(){
		render("index.jsp");
	}
	@Before(DeptLogInterceptor.class)
	public void list(){
		//这边应该传一个 parent id
		User user = getSessionAttr(GlobalKey.CurrentUser);
		Long deptId = user.getLong("dept_id");
		System.out.println("deptId:"+deptId);
		String sql = "select t.id,t.name,t.description,t.parent_id,t.order_id from sys_dept t where t.id=?";
		Dept dept = Dept.dao.findFirst(sql,deptId);
		Long id = dept.getLong("parent_id");
		
		
		Long userId = user.getLong("id");
		
		UserRole menu = UserRole.dao.findFirst("select * from sys_user_role where user_id =?" , userId);
		Role role = Role.dao.findFirst("select * from sys_role where id =?", menu.getLong("role_id"));
		String roleName = role.getStr("name");
		if(roleName.equals("系统管理员")){
			renderJson(JsonKit.toJson(deptService.getTreeGrid("0")));
		}else{
			renderJson(JsonKit.toJson(deptService.getTreeGrid(Long.toString(id))));
		}
	}
	@Before(DeptLogInterceptor.class)
	public void add(){
		
		Map<String, Boolean> result = new HashMap<String, Boolean>();
		User user = getSessionAttr(GlobalKey.CurrentUser);
		Long userId = user.getLong("id");
		boolean is  = RolePower.dao.getRole(userId , 11);
		if(!is){
			result.put("flag", false);
			renderJson(JsonKit.toJson(result));
			return;
		}
		
		Dept dept = new Dept();
		dept.set("name", getPara("name"));
		dept.set("description", getPara("description"));
		dept.set("parent_id", getPara("parent_id"));
		dept.set("order_id", getPara("order_id"));
		
		if (deptService.create(dept)){		
			result.put("flag", true);
		}else{
			result.put("flag", false);
		}
		renderJson(JsonKit.toJson(result));
	}
	@Before(DeptLogInterceptor.class)
	public void edit(){
		
		Map<String, Boolean> result = new HashMap<String, Boolean>();
		User user = getSessionAttr(GlobalKey.CurrentUser);
		Long userId = user.getLong("id");
		boolean is  = RolePower.dao.getRole(userId , 12);
		if(!is){
			result.put("flag", false);
			renderJson(JsonKit.toJson(result));
			return;
		}
		
		Dept dept = deptService.findById(getPara("id"));
		dept.set("name", getPara("name"));
		dept.set("description", getPara("description"));
		dept.set("order_id", getPara("order_id"));
		if(deptService.update(dept)){
			result.put("flag", true);
		}else{
			result.put("flag", false);
		}
		renderJson(JsonKit.toJson(result));
	}
	@Before(DeptLogInterceptor.class)
	public void delete(){
		Map<String, Object> result = new HashMap<String, Object>();
		User user = getSessionAttr(GlobalKey.CurrentUser);
		Long userId = user.getLong("id");
		boolean is  = RolePower.dao.getRole(userId , 13);
		if(!is){
			result.put("flag", false);
			result.put("Msg", "删除失败，请检查权限");
			renderJson(JsonKit.toJson(result));
			return;
		}
		
		boolean flag = true;
		String Msg = "";
		if (deptService.existsDept(getPara("id"))){
			flag = false;
			Msg = "该部门存在下属部门，不能删除！";
		}
		if (deptService.existsUser(getPara("id"))){
			flag = false;
			Msg = "该部门已有用户，不能删除！";
		}
		if (flag==true) {
			if(deptService.delete(getPara("id"))){
				Msg = "删除成功！";
			}else{
				flag=false;
				Msg = "删除失败！";
			}
		}
		result.put("flag", flag);
		result.put("Msg", Msg);
		renderJson(JsonKit.toJson(result));
	}
	public void upparentid(){
		Dept dept = deptService.findById(getPara("deptid"));
		dept.set("parent_id", getPara("parentid"));
		if(deptService.update(dept)){
			Map<String, Boolean> result = new HashMap<String, Boolean>();
			result.put("flag", true);
			renderJson(JsonKit.toJson(result));
		}else{
			Map<String, Boolean> result = new HashMap<String, Boolean>();
			result.put("flag", false);
			renderJson(JsonKit.toJson(result));
		}
	}
	
	
	//单位加锁
	@Before(DeptLogInterceptor.class)
	public void lock(){
		
		Map<String, Object> result = new HashMap<String, Object>();
		boolean flag = true;
		String Msg = "";
		String lockId = getPara("id");
		boolean locked = deptService.isLock(lockId);
		if(locked){
			//已加锁
			flag = false;
			Msg = "该部门已锁定";
		}
		
		if(flag){//开始加锁
			
			if(deptService.lock(lockId)){
				flag = true;
				Msg = "该部门锁定成功";
			}else{
				flag = false;
				Msg = "该部门锁定失败";
			}
			
		}
		
		result.put("flag", flag);
		result.put("Msg", Msg);
		renderJson(JsonKit.toJson(result));
	}
	//单位解锁
	@Before(DeptLogInterceptor.class)
	public void unLock(){
		
		Map<String, Object> result = new HashMap<String, Object>();
		boolean flag = true;
		String Msg = "";
		String lockId = getPara("id");
		boolean locked = !deptService.isLock(lockId);
		if(locked){
			//已加锁
			flag = false;
			Msg = "该部门未锁定";
		}
		
		if(flag){//开始加锁
			
			if(deptService.unLock(lockId)){
				flag = true;
				Msg = "该部门解锁成功";
			}else{
				flag = false;
				Msg = "该部门锁定失败";
			}
			
		}
		
		result.put("flag", flag);
		result.put("Msg", Msg);
		renderJson(JsonKit.toJson(result));
		
	}
	

}
